/*kvan
 *2014.5.12
 */
package com.taxsystem.likaifan.vehicletaxcheck;

import java.io.InputStream;
import java.net.URL;
import java.util.concurrent.ExecutionException;

import org.json.JSONException;
import org.json.JSONObject;

import com.main.taxsystem.R;
import com.taxsystem.likaifan.taxcompute.IndividualIncomeTaxCompute;
import com.taxsystem.likaifan.taxcompute.SecondHandHouseCompute;
import com.taxsystem.likaifan.taxcompute.SecondHandHouseResult;

import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Spinner;
import android.widget.TextView;
import android.widget.Toast;

public class VehicleTaxCheck extends Activity {

	public final static String RECI_COAST = "sw.system.HTTUTIL";
	private String result = "{\"message\": \"OK\",\"serverTime\": \"2014-06-10 12:14:23\",\"data\": {\"retCode\": \"1\",\"queryMsg\": \"查询成功！\",\"taxInfoList\": [{\"xh\": \"1\",\"year\": \"2014\",\"yzje\": \"420.00\",\"znje\": \"0.00\",\"fkje\": \"0.00\",\"sfbj\": \"0\"}]},\"code\": \"0\"}";
		
	
	
	final int MY_NOTICE = 0x111;
	final int NAV_SETTING = 0x112;
	final int ACCOUNT_SETTING = 0x113;
	final int INDIVIDUAL_SETTING = 0x114;
	
	private ImageView v_code;
	private EditText vehicle_number;
	private EditText engine_number;
	private EditText iden_code;
	private Bitmap bitmap;
	
	Handler handler = new Handler()
	{
		public void handleMessage(Message msg)
		{
		if(msg.what == 0x123)
		{
			v_code.setImageBitmap(bitmap);
		}
		}
	};
	
	private static final String[] type ={"小轿车","卡车","客车"};
	
	private Button button;
	
	private String requestUrl;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.vehicle_tax_check);
		
		// 注册广播
		vehicle_number = (EditText) findViewById(R.id.vehicle_number);
		engine_number = (EditText) findViewById(R.id.engine_number);
		iden_code = (EditText) findViewById(R.id.iden_code);
		v_code = (ImageView) findViewById(R.id.v_code);
		new Thread()
		{
			public void run()
			{
				try
				{
					URL url = new URL("http://mtaxtest.duapp.com/taxservice/ccsvalidcode.no");
					InputStream is = url.openStream();
					bitmap = BitmapFactory.decodeStream(is);
					handler.sendEmptyMessage(0x123);
					is.close();
					
				}
				catch(Exception e)
				{
					e.printStackTrace();
					
				}
			}
		}.start();
	/*	
		vehicle_type.setPrompt("请选择车辆类型"); 
		ArrayAdapter<String> type_adapter = new ArrayAdapter<String>(this, android.R.layout.simple_spinner_item, type);
	    type_adapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);   
	    vehicle_type.setAdapter(type_adapter);  

	    vehicle_type.setOnItemSelectedListener(new Spinner.OnItemSelectedListener(){

	           public void onItemSelected(AdapterView<?> arg0, View arg1, int arg2, long arg3)
	           {

	       		//Toast msg = Toast.makeText(individualIncomeTaxCompute.this, "选择的是"+m_arr[arg2], Toast.LENGTH_LONG);
	       		//msg.show();
	        	   arg0.setVisibility(View.VISIBLE);

	           }

	           public void onNothingSelected(AdapterView<?> arg0)
	           {

	               //

	           }

	       });

		
		*/
		IntentFilter filter = new IntentFilter(RECI_COAST);
		BroadcastReceiver myrecive = new MyRecive();
		registerReceiver(myrecive, filter);
		Thread thread = new Thread() 
		{

			@Override
			public void run() 
			{
				Intent intent = new Intent(RECI_COAST);
				try {
					//获取服务器返回的信息
					String reslut = com.taxsystem.tool.HttUtil
							.getRequest(requestUrl);
					intent.putExtra("weatherinfo", reslut);
					//发送广播
					sendBroadcast(intent);
				} catch (InterruptedException e) {
					e.printStackTrace();
				} catch (ExecutionException e) {
					e.printStackTrace();
				}
			}

		};
	}

	private boolean isNoEmpty()
	{
		if(vehicle_number.length()<1||engine_number.length()<1||iden_code.length()<1)
		{	
			return false;
		}
		else
			return true;
	}
	public void check(View view)
	{
		if(isNoEmpty())
		{
		
		 String provinceCode = "01";
		 String region = "A";
		 String url = "http://mtaxtest.duapp.com/taxservice/cssquery?vehicleCode="+ vehicle_number.getText()+
				 "&engineCode="+engine_number.getText()+"&validCode="+iden_code.getText()+"&provinceCode="+provinceCode+"&region="+region;
		 		
			
		 Log.d("duck",url);
		 /*
		 String result = null;
		 try {
			Log.d("test", "befu"); 
			result = com.taxsystem.tool.HttUtil.getRequest(url);
			Log.d("test",result);
		} catch (InterruptedException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (ExecutionException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		*/
		 
		 Intent intent = new Intent(this, VehicleTaxCheckResult.class);
		 intent.putExtra("result", result);
		 startActivity(intent);
		}
		else
		{
			Toast msg = Toast.makeText(VehicleTaxCheck.this, "请把信息填写完整再查询", Toast.LENGTH_LONG);
			msg.show();
		}
	}
	// 定义BroadcastReceiver 接收返回的信息
	private class MyRecive extends BroadcastReceiver {

		@Override
		public void onReceive(Context context, Intent intent) {
			try {
				//获取JSONObject对象
				JSONObject jsonobject = new JSONObject(
						intent.getStringExtra("weatherinfo"));
				JSONObject jsoncity = new JSONObject(
						jsonobject.getString("weatherinfo"));
				
			} catch (JSONException e) {
				e.printStackTrace();
			}
		}

	}


	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		//getMenuInflater().inflate(R.menu.activity_mian, menu);
		menu.add(0, MY_NOTICE, 0, "我的通知");
		menu.add(0, NAV_SETTING, 0, "导航设置");
		menu.add(0, ACCOUNT_SETTING, 0, "账号设置");
		menu.add(0, INDIVIDUAL_SETTING, 0, "个性化设置");
		return true;
	}
	
	@Override
	public boolean onOptionsItemSelected(MenuItem mi)
	{
		
		
		//判断单击的是哪个菜单项，并针对性的作出响应。
		switch (mi.getItemId())
		{
			case android.R.id.home:
				this.finish();
				break;
			case MY_NOTICE:
				Log.d("hehe","heh");
				Toast toast1 = Toast.makeText(getApplicationContext(),
					     "MY_NOTICE", Toast.LENGTH_SHORT);
				toast1.show();
				break;
			case NAV_SETTING:
				Toast toast2 = Toast.makeText(getApplicationContext(),
					     "NAV_SETTING", Toast.LENGTH_SHORT);
				toast2.show();;
				break;
			case ACCOUNT_SETTING:
				Toast toast3 = Toast.makeText(getApplicationContext(),
					     "ACCOUNT_SETTING", Toast.LENGTH_SHORT);
				toast3.show();
				break;
			case INDIVIDUAL_SETTING:
				Toast toast4 = Toast.makeText(getApplicationContext(),
					     "INDIVIDUAL_SETTING", Toast.LENGTH_SHORT);
				toast4.show();
				break;

		}
		return true;
	}

}
